package com.sevntu.apvs.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.jgoodies.forms.factories.DefaultComponentFactory;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import com.sevntu.apvs.duelement.Element;
import com.sevntu.apvs.duelement.FindCreateElement;

public class ElemetsInfo extends JFrame {

	private static final long serialVersionUID = 1L;

	private static ElemetsInfo frame;
	private JPanel contentPane;
	private JScrollPane scrollPaneElementInfo;
	private JTextArea textAreaElements;
	private JTextArea textAreaElementInfo;
	private final FindCreateElement elementsLib;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void init() {
		EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					if (frame == null) {
						frame = new ElemetsInfo();
					}
					frame.fillData();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ElemetsInfo() {
		elementsLib = new FindCreateElement();
		initializeFrame();
		fillData();
		setVisible(true);
	}

	private void fillData() {
		String[] elements = getElements();
		fillElemetsList(elements);
		setComboBoxData(elements);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initializeFrame() {
		setTitle("Components Info");
		setBounds(100, 100, 450, 434);
		setMinimumSize(new Dimension(300, 350));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FormLayout(new ColumnSpec[] { ColumnSpec.decode("min:grow"),
				ColumnSpec.decode("max(110dlu;pref)"), ColumnSpec.decode("left:pref:grow"), }, new RowSpec[] {
				RowSpec.decode("pref:grow"), FormFactory.PREF_ROWSPEC, RowSpec.decode("pref:grow(2)"), }));

		JScrollPane scrollPaneElements = new JScrollPane();
		scrollPaneElements.setViewportBorder(new TitledBorder(null, "Available Elemets", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		scrollPaneElements.setBorder(null);
		contentPane.add(scrollPaneElements, "1, 1, 3, 1, fill, fill");

		textAreaElements = new JTextArea();
		textAreaElements.setEditable(false);
		textAreaElements.setTabSize(12);
		textAreaElements.setColumns(1);
		textAreaElements.setLineWrap(true);
		textAreaElements.setWrapStyleWord(true);
		textAreaElements.setFont(new Font("Monospaced", Font.PLAIN, 12));
		textAreaElements.setBackground(getContentPane().getBackground());
		textAreaElements.setSelectedTextColor(Color.WHITE);
		textAreaElements.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		scrollPaneElements.setViewportView(textAreaElements);

		JLabel lblFindElementInfo = DefaultComponentFactory.getInstance().createLabel("Find Element Info:");
		contentPane.add(lblFindElementInfo, "1, 2, right, default");

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				findElement((String) comboBox.getSelectedItem());
			}
		});
		comboBox.setMaximumRowCount(10);
		contentPane.add(comboBox, "2, 2, fill, default");

		scrollPaneElementInfo = new JScrollPane();
		scrollPaneElementInfo.setViewportBorder(new TitledBorder(null, "Element Info ", TitledBorder.LEADING,
				TitledBorder.TOP));
		scrollPaneElementInfo.setBorder(null);
		contentPane.add(scrollPaneElementInfo, "1, 3, 3, 1, fill, fill");

		textAreaElementInfo = new JTextArea();
		textAreaElementInfo.setFont(new Font("Monospaced", Font.PLAIN, 12));
		textAreaElementInfo.setEditable(false);
		textAreaElementInfo.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		scrollPaneElementInfo.setViewportView(textAreaElementInfo);
	}

	private void findElement(String element) {
		String title = "Element: ";
		Element findElement = elementsLib.FindElement(element);
		if (findElement != null) {
			title += findElement.name;
			// set Border to textArea with name of Element
			setTitleElementInfo(title);

			StringBuffer elementInfo = new StringBuffer();
			elementInfo.append("Name:\t\t\t");
			elementInfo.append(findElement.name);
			elementInfo.append("\n");

			elementInfo.append("Type of logic:\t\t");
			elementInfo.append(findElement.Component);
			elementInfo.append("\n");

			elementInfo.append("Element ID:\t\t");
			elementInfo.append(findElement.id);
			elementInfo.append("\n");

			elementInfo.append("Number of inputs:\t");
			elementInfo.append(findElement.Ninput);
			elementInfo.append("\n");

			elementInfo.append("Number of outputs:\t");
			elementInfo.append(findElement.Noutput);
			elementInfo.append("\n");

			elementInfo.append("Pin numbers of element:\t");
			elementInfo.append(findElement.Ncont);
			elementInfo.append("\n");

			elementInfo.append("Signal time generation:\t");
			elementInfo.append(findElement.time);
			elementInfo.append("\n");

			elementInfo.append("Truth table:\t\t");
			elementInfo.append(findElement.tabI);
			elementInfo.append("\n");

			textAreaElementInfo.setText(elementInfo.toString());
		}

	}

	/**
	 * @param title
	 */
	private void setTitleElementInfo(String title) {
		scrollPaneElementInfo.setViewportBorder(new TitledBorder(null, title));
		scrollPaneElementInfo.repaint();
	}

	private void setComboBoxData(String[] data) {
		comboBox.removeAllItems();
		for (String s : data) {
			comboBox.addItem(s);
		}
		comboBox.repaint();
	}

	protected String[] getElements() {
		List<String> elements = elementsLib.CreateListElement();
		String[] arrayElements = elements.toArray(new String[0]);
		return arrayElements;
	}

	/**
	 * @param elements
	 */
	protected void fillElemetsList(String[] elementsArray) {
		StringBuffer elementString = new StringBuffer();
		int count = 1;
		for (String s : elementsArray) {
			elementString.append(s);
			if (count % 2 == 0) {
				elementString.append("\n");
			} else {
				if (count != elementsArray.length) {
					elementString.append("\t\t");
				}
			}
			count++;
		}
		textAreaElements.setText(elementString.toString());
	}

}
